﻿#region Using

using System.Collections.Generic;

using GameCommon.ComponentInterfaces;

using Menu.Components;

using Microsoft.Xna.Framework;

#endregion

namespace Menu.Screens.ConnectionScreen
{
    internal class ConnectingTextAnimation : IAnimation
    {
        private const float Delay = 0.2f;

        private readonly IList<string> chars;

        private readonly Text connecting;

        private int charIndex;

        private double elapsedTime;

        public ConnectingTextAnimation(Text text)
        {
            this.connecting = text;
            this.chars = new List<string> { "/", "-", "\\", "|" };
        }
        
        public bool IsFinished { get; private set; }

        public void Update(GameTime gameTime)
        {
            this.elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;

            if (this.elapsedTime > Delay && this.connecting.Value.Length > 0)
            {
                this.connecting.Value = this.connecting.Value.Substring(0, this.connecting.Value.Length - 1);
                this.connecting.Value += this.GetNextChar();
                this.elapsedTime = 0;
            }
        }

        private string GetNextChar()
        {
            while (true)
            {
                if (this.charIndex < this.chars.Count)
                {
                    this.charIndex++;
                    return this.chars[this.charIndex - 1];
                }

                this.charIndex = 0;
            }
        }
    }
}